#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <stdio.h>

void *text = 0x10000;
void *data = 0x20000;

extern char _binary_mod_bin_start;
extern char _binary_mod_bin_end;
extern int _binary_mod_bin_size;

int main() {
  int (*f)(int, int) = (int (*)(int, int))text;
  int *glob = (int *)data;
  int res;
  
  printf ("%p\n", &_binary_mod_bin_start);
  printf ("%p\n", &_binary_mod_bin_end);
  printf ("%p\n", &_binary_mod_bin_size);
  
  
  if (MAP_FAILED == 
	mmap(text, 1024, PROT_EXEC | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, 0, 0)
  ) {
    printf ("mmap 1 failed\n");
    return 1;
  }
  
  if (MAP_FAILED == mmap(data, 1024, PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, 0, 0)) {
    printf ("mmap 2 failed\n");
    return 1;
  }
  
  memcpy (text, &_binary_mod_bin_start, (unsigned long)&_binary_mod_bin_size);
  
   res = f(10, 3);
  
   printf ("res: %d, glob: %d\n", res, *glob);
  
  
    
  munmap(text, 1024);
  munmap(data, 1024);
  
  return 0;
}